package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.ExamsJdbc;
import com.wuseguang.springcloud.dao.repository.ExamsRepository;
import com.wuseguang.springcloud.entity.Exams;
import com.wuseguang.springcloud.service.ExamsService;
import com.wuseguang.springcloud.util.PaginationUtil;
import com.wuseguang.springcloud.util.PagingTool;
import com.wuseguang.springcloud.util.Result;
import com.wuseguang.springcloud.util.UpdateTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 考试表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-04-28
 */
@Service
public class ExamsServiceImpl implements ExamsService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());



    @Autowired
    private ExamsRepository examsRepository;
    @Autowired
    private ExamsJdbc examsJdbc;


    @Override
    public Result findExamInformation(String deviceid) {

        logger.info("查询考试信息");
        Map<String, Object> stringObjectMap4 = examsJdbc.queryDevice(deviceid);

        String psql = "select id string from examination_room where school_id=? and pattern = 1";
        //考场id
        String  erId = examsJdbc.queryString(psql,stringObjectMap4.get("school_id"));



        String sql = "SELECT e.id, e.classroom_id, e.examination_room_no, e.grade_id, " +
                " e.course_id, e.teacher_id, date_format( e.start_time, '%Y-%m-%d %T' ) start_time, " +
                " date_format( e.end_time, '%Y-%m-%d %T' ) end_time, e.examination_room_id, e.school_id, " +
                " date_format( e.add_time, '%Y-%m-%d %T' ) add_time, cr.`name` classroomName, " +
                " cs.`name` courseName,e.seat_url ,g.name gradeName   " +
                " FROM exams e LEFT JOIN classroom cr ON ( cr.id = e.classroom_id ) " +
                " LEFT JOIN grade g ON ( e.grade_id = g.id ) " +
                " LEFT JOIN course cs ON ( cs.id = e.course_id ) " +
                " WHERE e.classroom_id =?  and e.examination_room_id = ? ";
        List<Map<String,Object>> list = examsJdbc.queryListMap(sql,stringObjectMap4.get("classroomid"),erId);

        //查询这个考试的监考老师

        for (Map<String,Object> map : list){
           String teacherId  = map.get("teacher_id").toString();
            sql = "select id,name,head_portrait from teacher where id in ("+teacherId+")";
            List<Map<String,Object>> teacherList = examsJdbc.queryListMap(sql);
            map.put("teacherList",teacherList);
        }
        return new Result(list);
    }
}
